/**
 * Syg at 17.03.2013 for Pikovskii
 */
package intf.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.ListIterator;

import intf.ILine;
import intf.IPLine;
import intf.IPoint;

/**
 * @author Syg
 *
 */
public class PLineImpl implements IPLine
{
	private static final int DEFAULT_SIZE = 8;
	private ArrayList<IPoint> m_list;
	private boolean m_enclosed;
	
	/**
	 * @param size
	 */
    public PLineImpl( int size)
    {
    	m_list = new ArrayList<IPoint>(size);
    }
    
    /**
	 * @param list
	 * @param enclosed if true line is an enclosed polygon else is a simple polyline
	 */
    public PLineImpl( int size, boolean enclosed)
    {
	    this(size);
	    this.m_enclosed = enclosed;
    }

	/**
	 * 
	 */
    public PLineImpl()
    {
    	m_list = new ArrayList<IPoint>(DEFAULT_SIZE);
    }

	public void setEnclosed(boolean enclosed)
    {
    	m_enclosed = enclosed;
    }
    
	/* (non-Javadoc)
	 * @see intf.IPLine#size()
	 */
    @Override
    public int size()
    {
	    return m_list.size();
    }

	/* (non-Javadoc)
	 * @see intf.IPLine#line(int)
	 */
    @Override
    public ILine line( int index ) throws ArrayIndexOutOfBoundsException
    {
    	if ( (index == (size()-1)) & m_enclosed) // enclosing line requested
    	    return new LineImpl(m_list.get(index), m_list.get(0), (byte) 0);
	    return new LineImpl(m_list.get(index), m_list.get(index + 1), (byte) 0);
    }

	/* (non-Javadoc)
	 * @see intf.IPLine#getIterator()
	 */
    @Override
    public ListIterator<ILine> getIterator()
    {
	    // TODO Auto-generated method stub
	    return null;
    }

	/* (non-Javadoc)
	 * @see intf.IPLine#split(int, intf.IPoint)
	 */
    @Override
    public void split( int index, IPoint pnt )
    {
    	insertBefore(index, pnt);
    }

	/* (non-Javadoc)
	 * @see intf.IPLine#insertBefore(int, intf.IPoint)
	 */
    @Override
    public void insertBefore( int index, IPoint point )
    {
    	m_list.add(index, point);
    }

	/* (non-Javadoc)
	 * @see intf.IPLine#append(intf.IPoint)
	 */
    @Override
    public void append( IPoint point )
    {
	    m_list.add(point);
    }
    
    public void append( double x, double y)
    {
	    m_list.add(new PointImpl(x, y));
    }
    

	/* (non-Javadoc)
	 * @see intf.IPLine#lineCount()
	 */
    @Override
    public int lineCount()
    {
	    return size() - 1 + (m_enclosed ? 1 : 0);
    }

	/* (non-Javadoc)
	 * @see intf.IPLine#isEnclosed()
	 */
    @Override
    public boolean isEnclosed()
    {
	    return m_enclosed;
    }

	/* (non-Javadoc)
	 * @see intf.IPLine#isInterior(intf.IPoint)
	 */
    @Override
    public boolean isInterior( IPoint pnt )
    {
	    // TODO Auto-generated method stub
	    return false;
    }

}
